package com.yunang.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunang.oa.common.R;
import com.yunang.oa.service.OaDepartmentPlusService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.*;
import java.util.List;

import com.yunang.oa.entity.OaDepartment;
import com.yunang.oa.service.OaDepartmentService;


@RestController
@RequestMapping("/v1/oa/department")
public class OaDepartmentController {

    private final OaDepartmentPlusService oaDepartmentPlusService;

    private final OaDepartmentService oaDepartmentService;


    @Autowired
    public OaDepartmentController(OaDepartmentPlusService oaDepartmentPlusService, OaDepartmentService oaDepartmentService) {
        this.oaDepartmentPlusService = oaDepartmentPlusService;
        this.oaDepartmentService = oaDepartmentService;
    }

    @ApiOperation(value = "增加一个部门")
    @PostMapping("/add")
    public R addOaDepartment(@RequestBody(required = true) OaDepartment department) {
        boolean b = oaDepartmentPlusService.save(department);

        return R.ok(b);
    }

    @ApiOperation(value = "根据部门id进行部门删除")
    @DeleteMapping("/remove/{id}")
    public R removeDepartmentById(@PathVariable("id") Long id) {
        boolean b = oaDepartmentPlusService.removeById(id);
        return R.ok(b);
    }

    @ApiOperation(value = "进行部门信息修改")
    @PostMapping("/save")
    public R saveOaDepartment(@RequestBody(required = true) OaDepartment department) {
        boolean b = oaDepartmentPlusService.updateById(department);
        return R.ok(b);
    }


    @ApiOperation(value = "查找所有的部门")
    @GetMapping("/findAll")
    public R getOaDepartmentList() {
        List<OaDepartment> list = oaDepartmentPlusService.list();
        return R.ok(list);
    }

    @ApiOperation(value = "通过部门id查找部门")
    @GetMapping("/getById/{id}")
    public R getOaDepartmentById(@PathVariable("id") Long id) {

        QueryWrapper<OaDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("department_id", id);
        List<OaDepartment> list = oaDepartmentPlusService.list(wrapper);
        return R.ok(list);
    }

    @ApiOperation(value = "通过部门名称查找部门")
    @GetMapping("/getByName/{name}")
    public R getOaDepartmentByName(@PathVariable("name") String name) {

        QueryWrapper<OaDepartment> wrapper = new QueryWrapper<>();
        wrapper.like("department_name",name);

        List<OaDepartment> list = oaDepartmentPlusService.list(wrapper);

        return R.ok(list);
    }


    @ApiOperation(value = "查找所有部门完整信息")
    @GetMapping("/getWithUser")
    public R getOaDepartmentUserList(){

        List<OaDepartment> departmentWithUserInfo = oaDepartmentService.getDepartmentWithCharger();
        return R.ok(departmentWithUserInfo);

    }


}
